package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.OrderItem;
import cn.wolfcode.trip.base.service.IOrderItemService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author by mycp
 * @Classname OrdersController
 * @Description TODO 订单处理器
 * @Date 2018/11/16 17:20
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrderItemService orderItemService;

    /**
     * 新增用户积分兑换商品订单
     * 动作: POST
     * 参数: 订单对象的参数
     * 接口: /orders
     */
    @PostMapping
    public Object addOrder(OrderItem orderItem) {
        JsonResult result = new JsonResult();
        orderItemService.saveOrder(orderItem);
        return result;
    }

    /**
     * 获取指定用户的订单信息
     * 动作: GET
     * 参数: userId = xxx  订单状态: status = 1
     * 接口: /orders/{userId}
     */
    @GetMapping("/{userId}/orders")
    public Object getOrders(@PathVariable Long userId, Long status) {
        return orderItemService.getOrders(userId, status);
    }


    /**
     * 用户下单兑换商品处理
     * 动作: PUT
     * 参数: 订单id
     */
    @PutMapping
    public Object payOrder(Long orderId) {
        JsonResult result = new JsonResult();
        orderItemService.payOrder(orderId);
        return result;
    }


    /**
     * 查询id 的订单
     * 动作: GET
     */
    @GetMapping("/{orderId}")
    public Object getOrder(@PathVariable Long orderId) {
        OrderItem orderItem = orderItemService.getOrderByOrderId(orderId);
        return orderItem;
    }


    /**
     * 用户取消订单
     * 动作: PUT
     * 参数: 订单id
     */
    @DeleteMapping("/{orderId}")
    @ResponseStatus(value= HttpStatus.NO_CONTENT)
    public void cancelOrder(@PathVariable Long orderId) {
        orderItemService.cancelOrder(orderId);
    }

}
